#define DEBUG
#include <cstdio>
#define maxh 4000
#define maxw 4000

using namespace std;

inline int max(int a, int b) {
  return a>b ? a : b;
}

int main() {
#ifdef DEBUG
  freopen("3319.in", "r", stdin);
  freopen("3319.out", "w", stdout);
#endif

  static char map[maxh+1][maxw+1];
  int h, w;
  scanf("%d %d ", &h, &w);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      map[i][j]=getchar();
    }
    getchar();
  }

  const int fx[4][2]={{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
  static int f[maxh+1][maxw+1], que[2][maxh*maxw+1][2];
  que[1][1][0]=que[1][1][1]=1;
  f[1][1]=1;
  int ans=1;
  for (int tail=0, nextail=1, now, next, x, y, fn, nx, ny; nextail; ans++) {
    tail=nextail, nextail=0;
    now=ans%2, next=(ans+1)%2;
    for (int i=1; i<=tail; i++) {
      x=que[now][i][0], y=que[now][i][1], fn=f[x][y];
      for (int i=0; i<4; i++) {
	nx=x+fx[i][0], ny=y+fx[i][1];
	if (nx>0 && ny>0 && nx<=h && ny<=w && !f[nx][ny]) {
	  if (map[nx][ny]==map[x][y]) {
	    f[nx][ny] = fn;
	    tail++;
	    que[now][tail][0]=nx;
	    que[now][tail][1]=ny;
	  } else if (map[nx][ny]!='.') {
	    f[nx][ny] = fn+1;
	    nextail++;
	    que[next][nextail][0]=nx;
	    que[next][nextail][1]=ny;
	  }
	}
      }
    }
  }

  printf("%d", ans-1);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
